package com.centling.smartSealForPhone.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.application.SmartSeal;
import com.centling.smartSealForPhone.constants.SPConstants;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.fionera.base.util.MD5Util;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.SPUtil;
import com.centling.smartSealForPhone.utils.ShowToast;

import java.util.HashMap;
import java.util.Map;

public class ModifySelfPassActivity extends TitleBarActivity implements View.OnClickListener {
    private EditText oldPass;
    private EditText newPass;
    private EditText newPassRepeat;
    private TextView sure;
    private TextView cancel;

    private String oldPwd;
    private String newPwd;
    private String newPwdRepeat;
    private String userId;
    private boolean fromLogin = false;
    private TextView mTvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_self_pass);

        initView();
        fromLogin = getIntent().getBooleanExtra("fromLogin", false);
        userId = SPUtil.getString(SPConstants.USER_ID);
        if (fromLogin) {
            setTitleBarVisibility(View.GONE);
            mTvTitle.setVisibility(View.VISIBLE);
            mTvTitle.setText("初次登陆,请修改密码");
            mTvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.blue1));
        } else {
            setTitleBarVisibility(View.VISIBLE);
            setTitleBarText("修改密码");
            mTvTitle.setVisibility(View.GONE);
        }
    }

    public void initView() {
        oldPass = (EditText) findViewById(R.id.set_pass_old);
        newPass = (EditText) findViewById(R.id.set_pass_new);
        newPassRepeat = (EditText) findViewById(R.id.set_pass_repeat);
        sure = (TextView) findViewById(R.id.set_pass_sure);
        cancel = (TextView) findViewById(R.id.set_pass_cancel);
        mTvTitle = (TextView) findViewById(R.id.self_pass_tv_title);
        sure.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        InputMethodManager inputMethodService = (InputMethodManager) mContext.getSystemService(
                Context.INPUT_METHOD_SERVICE);
        inputMethodService.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
        if (view.getId() == R.id.set_pass_sure) {
            if (checkPasswordReasonable()) {
                mProcessDialog.setTitle("正在修改密码").showNormal();
                Map<String, String> param = new HashMap<>();
                param.put("userId", userId);
                param.put("oldPassword", MD5Util.md5(oldPwd));
                param.put("nnewPassword", MD5Util.md5(newPwd));
                putJsonEnqueue(UrlConstants.SET_SELF_PASS, param, new OkHttpUtil.NetCallBack() {
                    @Override
                    public void onSucceed(String json) {
                        mProcessDialog.dismiss();
                        ShowToast.shortTime("修改密码成功");
                        if (fromLogin) {
                            Intent intent = new Intent();
                            intent.putExtra("newpsw", newPwd);
                            setResult(RESULT_OK, intent);
                            finish();
                            return;
                        }
                        AtyManager.INSTANCE.finishAllActivities();
                        String ip = SmartSeal.getInstance().getIp();
                        SPUtil.clearAll();
                        SmartSeal.getInstance().changeIp(ip);
                        Intent intent = new Intent(ModifySelfPassActivity.this, LoginActivity.class);
                        intent.putExtra("pass_flag", 1);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onFailed(String reason) {
                        mProcessDialog.dismiss();
                        ShowToast.shortTime(reason);
                    }
                });

            }
        } else if (view.getId() == R.id.set_pass_cancel) {
            finish();
        }
    }

    public boolean checkPasswordReasonable() {
        oldPwd = oldPass.getText().toString();
        newPwd = newPass.getText().toString();
        newPwdRepeat = newPassRepeat.getText().toString();

        if (oldPwd == null || "".equals(oldPwd)) {
            ShowToast.shortTime("原密码不能为空");
            return false;
        } else if (newPwd == null || "".equals(newPwd)) {
            ShowToast.shortTime("新密码不能为空");
            return false;
        } else if (newPwdRepeat == null || "".equals(newPwdRepeat)) {
            ShowToast.shortTime("请再次输入新密码");
            return false;
        } else if (!newPwd.equals(newPwdRepeat)) {
            ShowToast.shortTime("两次输入的密码不一致");
            return false;
        } else if (oldPwd.length() < 6) {
            ShowToast.shortTime("原密码小于6位");
            return false;
        } else if (newPwd.length() < 6) {
            ShowToast.shortTime("新密码小于6位");
            return false;
        } else if (newPwdRepeat.length() < 6) {
            ShowToast.shortTime("新密码小于6位");
            return false;
        } else {
            return true;
        }
    }
}
